package com.touchapk.javafx;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

import javafx.application.Platform;
import javafx.collections.FXCollections;
import javafx.fxml.FXML;
import javafx.scene.control.CheckBox;
import javafx.scene.control.ComboBox;
import javafx.stage.DirectoryChooser;



/**
 * @description
 * @author yangkeyun
 * @email coyun@qq.com
 * @date 2016年8月7日
 * @version V1.0
 */
public class SelectController implements DialogController {

	private MainController mainController;

	public SelectController() {
	}

	public SelectController(MainController mainController) {
		this.mainController = mainController;
	}

	private FXMLDialog dialog;

	@Override
	public void setDialog(FXMLDialog dialog) {
		this.dialog = dialog;
	}

	@FXML
	private ComboBox pathComboBox;

	@FXML
	private CheckBox rememberCheckBox;

	@FXML
	public void browserFunc() {

		final DirectoryChooser directoryChooser = new DirectoryChooser();
		
		final File selectedDirectory = directoryChooser.showDialog(dialog);
		
		if (selectedDirectory != null) {
		
			pathComboBox.setItems(FXCollections.observableArrayList(selectedDirectory.getAbsolutePath()));
			
			pathComboBox.getSelectionModel().select(0);
		}

	}

	@FXML
	public void okFunc() {
		
		String path=pathComboBox.getSelectionModel().getSelectedItem().toString();
		
		if("".equals(path)){
			MTooltip.show(dialog, pathComboBox,this.mainController.getI18nBundle().getString("select.title"));
			return;
		}else{
			File file=new File(path);
			if(!file.exists()){
				if(!file.mkdirs()){
					MTooltip.show(dialog, pathComboBox, this.mainController.getI18nBundle().getString("select.title"));
				}
			}
		}
		
		this.mainController.setPath(path);
		
		
		setProperties("path",path,"remember",rememberCheckBox.isSelected()?"1":"0");
		
		Platform.runLater(new Runnable() {
            @Override
            public void run() {
            	dialog.close();
        		
        		FXMLDialog tdialog = mainController.getTouchDialog();
        		tdialog.setTitle(mainController.getI18nBundle().getString("application.title"));
        		tdialog.show();
            }
		 });
		
		
		
	}

	@FXML
	public void cancelFunc() {
		 Platform.exit();
	}

	/**
	 * Initializes the controller class.
	 */
	@FXML
	// This method is called by the FXMLLoader when initialization is complete
	void initialize() {
		config();
	}
	
	public void config(){
		
			String path=getProperties("path",System.getProperty("user.home"));
			
			String remember=getProperties("remember","0");
			
			boolean flag=false;
			
			if("1".equals(remember)&&!path.equals(System.getProperty("user.home"))){
				File file=new File(path);
				if(file.exists()){
					flag=true;
				}
			}
			
			if(flag){
				
				this.mainController.setPath(path);
				
				this.mainController.getTouchDialog().show();
				
			}else{
				
				pathComboBox.setItems(FXCollections.observableArrayList(path));
				
				pathComboBox.getSelectionModel().select(0);
			}
			
	}

	public static String getProperties(String key,String def){
		
		Properties pro = new Properties();
		
		File file = new File("config.ini");
		
		if (!file.exists()) {
			try {
				file.createNewFile();
			} catch (IOException e) {
	
			}
		}
		
		try {
			FileInputStream in = new FileInputStream(file);
			
			pro.load(in);
			
			String v = pro.getOrDefault(key, def).toString();
			
			return v;
			
		} catch (Exception e) {}
		
		return def;
	}
	
	public static void setProperties(String key,String value,String key1,String value1){
		Properties pro = new Properties();
		
		File file = new File("config.ini");
		
		if (!file.exists()) {
			try {
				file.createNewFile();
			} catch (IOException e) {
	
			}
		}
		
		try {
			FileOutputStream out = new FileOutputStream(file);
			pro.setProperty(key, value);
			pro.setProperty(key1, value1);
			pro.store(out, null);
			//out.flush();
			out.close();
		} catch (Exception e) {}
	}
	
}
